package com.tapstitch.scm.pms.web.controller.system;


import com.bluebuck.quickboot.core.api.dto.ApiResult;
import com.tapstitch.scm.pms.web.controller.AdminBaseController;
import com.bluebuck.sso.client.admin.auth.model.LoginUser;
import com.bluebuck.sso.client.model.SsoUserInfo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author yy
 */
@RestController
@RequestMapping("/system")
public class SystemController extends AdminBaseController {
    

    /**
     * 分页查询用户列表
     * @return 用户列表
     */
    @GetMapping("/jump")
    public ApiResult<?> jump() {
        LoginUser loginUser = currentUser();
        //TODO 由于sso没有封装进去SsoUserInfo信息，因此拿不到跳转路径
//        HornbillLoginUser hornbillLoginUser = new HornbillLoginUser();
//        BeanUtils.copyProperties(hornbillLoginUser, loginUser);
//        hornbillLoginUser.setSystemRouters(getSystemRouters(loginUser.get));
        return ApiResult.successResult(loginUser);
    }

    private List<HornbillLoginUser.RouterSystemView> getSystemRouters(List<SsoUserInfo.ClientResource> clientList) {
        List<HornbillLoginUser.RouterSystemView> views = new ArrayList<>(clientList.size());
        Long id = 999999L;
        for (SsoUserInfo.ClientResource client : clientList) {
            HornbillLoginUser.RouterSystemView view = new HornbillLoginUser.RouterSystemView();
            view.setName(client.getClientName());
            String clientHost = client.getClientHost();
            String replace1 = clientHost.replace("/dev-api", "").replace("/test-api", "").replace("/prod-api", "");
            String replace2 = replace1.replace("com/", "com");
            view.setPath(replace2);
            view.setNextPath(replace2);
            view.setId(id);
            List<SsoUserInfo.Resource> resourceList = client.getResourceList();
            if (CollectionUtils.isEmpty(resourceList)) {
                continue;
            }
            Map<Long, List<SsoUserInfo.Resource>> parentMap = resourceList.stream().collect(Collectors.groupingBy(SsoUserInfo.Resource::getParentId));
            buildSystemRouter(-1L, view, parentMap);
            view.setChildren(view.getChildren());
            views.add(view);
            id++;
        }
        return views;
    }

    private void buildSystemRouter(Long parentId, HornbillLoginUser.RouterSystemView parent, Map<Long, List<SsoUserInfo.Resource>> parentMap) {
        List<SsoUserInfo.Resource> children = parentMap.getOrDefault(parentId, new ArrayList<>());
        for (SsoUserInfo.Resource entity : children) {
            // 按钮
            if (entity.getType().equals(2)) {
                continue;
            }
            HornbillLoginUser.RouterSystemView view = new HornbillLoginUser.RouterSystemView();
            String realPath = parent.getNextPath() + "/" + entity.getPath();
            if (entity.getIsFrame()) {
                realPath = entity.getPath();
            }
            if (entity.getParentId() < 0) {
                realPath = parent.getPath();
            }
            view.setName(entity.getName());
            view.setId(entity.getId());
            view.setPath(realPath);
            view.setNextPath(parent.getPath() + "/" + entity.getPath());
            parent.addChild(view);
            buildSystemRouter(entity.getId(), view, parentMap);
        }
    }

}
